package com.koron.housing.staff.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.housing.staff.bean.dto.HousingStaffDTO;
import com.koron.housing.staff.bean.po.HousingStaff;
import com.koron.housing.staff.bean.query.HousingStaffQuery;
import com.koron.housing.staff.bean.vo.HousingStaffVO;
import com.koron.housing.staff.service.HousingStaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * 租户信息(HousingStaff)表控制层
 *
 * @author jzp
 * @since 2022-02-26 11:30:35
 */
@Api(tags = "租户信息接口")
@RestController
@RequestMapping("housingStaff")
public class HousingStaffController {
    /**
     * 服务对象
     */
    @Autowired
    private HousingStaffService housingStaffService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<HousingStaffDTO> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), housingStaffService, "getDtoById", HousingStaffDTO.class, id));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<PageInfo<HousingStaffVO>> page(PageQuery pageQuery, HousingStaffQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), housingStaffService, "page", PageInfo.class, pageQuery, query));
    }

    @ApiOperation(value = "根据租户账号查询租户信息")
    @GetMapping("account")
    public Response<HousingStaff> account(String account) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), housingStaffService, "getByAccount", HousingStaff.class, account));
    }

    @ApiOperation(value = "保存或更新")
    @PostMapping("save")
    public Response<String> save(@RequestBody HousingStaff housingStaff) {
        ADOConnection.runTask(getCurrDs(), housingStaffService, "save", Boolean.class, housingStaff);
        return Response.ok(housingStaff.getId());
    }

    @ApiOperation(value = "更新")
    @PutMapping("update")
    public Response<String> update(@RequestBody HousingStaff housingStaff) {
        ADOConnection.runTask(getCurrDs(), housingStaffService, "update", Boolean.class, housingStaff);
        return Response.ok(housingStaff.getId());
    }

    @ApiOperation(value = "续租")
    @PutMapping("renewal")
    public Response<String> renewal(@RequestBody HousingStaff housingStaff) {
        ADOConnection.runTask(getCurrDs(), housingStaffService, "renewal", Boolean.class, housingStaff);
        return Response.ok(housingStaff.getId());
    }

    @ApiOperation(value = "强制退租")
    @PutMapping("forceOut")
    public Response<String> forceOut(String id) {
        ADOConnection.runTask(getCurrDs(), housingStaffService, "forceOut", Boolean.class, id);
        return Response.ok(id);
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), housingStaffService, "deleteById", Boolean.class, id);
        return Response.ok();
    }

    @SneakyThrows
    @ApiOperation(value = "导出")
    @GetMapping("export")
    public void exportFile(HttpServletResponse response, HousingStaffQuery query) {
        List<HousingStaffVO> list = ADOConnection.runTask(getCurrDs(), housingStaffService, "list", List.class, query);

        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("租户信息", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), HousingStaffVO.class)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("租户信息")
                .doWrite(list);
    }

}
